#!/bin/awk
BEGIN{
  er=0;
  nrot=ARGV[1];
  sd[1]=ARGV[2]; #first argument is mirror
  sd[2]=ARGV[3]; #second argument is screw
#    print "DEBUG sym.awk:",sd[1],sd[2] > "/dev/stderr";
#    print ARGC,ARGV[1],ARGV[2];
  exit;
}
END{
  if(er==0){ nmi=1; nsc=1;
  for(i=0;i<=9;i++){s[1,i]=i}; # ten basis direction mult 2pi/10
  s[1,10]=1; # periodic direction
  s[1,11]=1; s[1,12]=1; s[1,13]=1; #these 3 are for "5", "m" and "s".
  if(nrot==10){
    for(i=2;i<=10;i++){ # 5-fold rotation
      for(j=0;j<=9;j++){
	k=(j+(i-1))%10;
	s[i,j]=s[1,k];
      };
      for(j=10;j<=13;j++){s[i,j]=s[1,j]};
      s[i,11]=i;
    };
  };
  if(nrot==5){
    for(i=2;i<=5;i++){ # 5-fold rotation
      for(j=0;j<=9;j++){
	k=(j+2*(i-1))%10;
	s[i,j]=s[1,k];
      };
      for(j=10;j<=13;j++){s[i,j]=s[1,j]};
      s[i,11]=i;
    };
  }
  nop=nrot;
  if(sd[1]==1){ # mirror
    nmi=2;
    nop0=nop; nop=nop*nmi;
    for(i=nop0+1;i<=nop;i++){
      for(j=0;j<=9;j++){
	k=(10-j)%10;
	s[i,j]=s[i-nop0,k];
      }
      for(j=10;j<=13;j++){s[i,j]=s[i-nop0,j]};
      s[i,12]=2;
    }
#	     print "mirror",nmi>"/dev/stderr";
  }
  if(sd[2]==1){ # 2-fold screw
    nsc=2;
    nop0=nop; nop=nop*nsc;
    for(i=nop0+1;i<=nop;i++){
      for(j=0;j<=9;j++){
	k=(j+5)%10;
	s[i,j]=s[i-nop0,k];
      }	
      for(j=10;j<=13;j++){s[i,j]=s[i-nop0,j]};
      s[i,13]=2; s[i,10]=-1*s[1,10];
    }
  }
  fname="INPUT/symmetry.def";
  print nop,nrot,nmi,nsc" # NOP Nrot Nmir Nscrew; [+s(1..10) dperp 5fold mirror 2fold_screw]">fname;
  for(i=1;i<=nop;i++){
    for(j=0;j<=9;j++){printf "%1i ",s[i,j]+1>>fname};
    printf "  %2i  ",s[i,10]>>fname;
    for(j=11;j<=13;j++){printf " %1i",s[i,j]>>fname};
    printf"\n">>fname;
  }
#	 print "# conversion to basis-5 code:">>fname;
#	 c[1]=1; c[2]=-4; c[3]=2; c[4]=-5; c[5]=3;
#	 c[6]=-1; c[7]=4; c[8]=-2; c[9]=5; c[10]=-3;
#	 for(i=1;i<=nop;i++){
#	     for(j=0;j<=9;j++){printf "%2i ",c[s[i,j]+1]>>fname};
#	     printf "  %2i  ",s[i,10]>>fname;
#	     for(j=11;j<=13;j++){printf " %1i",s[i,j]>>fname};
#	     printf"\n">>fname;
#	 }
  }
  print er;
}
